package com.dure.commonlib.util;

import android.text.TextUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * TimeUtils
 *
 * @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2013-8-24
 */
public class TimeUtils {

    public static final SimpleDateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static final SimpleDateFormat DATE_FORMAT_DATE = new SimpleDateFormat("yyyy-MM-dd");

    private TimeUtils() {
        throw new AssertionError();
    }

    /**
     * long time to string
     *
     * @param timeInMillis
     * @param dateFormat
     * @return
     */
    public static String getTime(long timeInMillis, SimpleDateFormat dateFormat) {
        return dateFormat.format(new Date(timeInMillis));
    }

    /**
     * long time to string, format is {@link #DEFAULT_DATE_FORMAT}
     *
     * @param timeInMillis
     * @return
     */
    public static String getTime(long timeInMillis) {
        return getTime(timeInMillis, DEFAULT_DATE_FORMAT);
    }

    /**
     * get current time in milliseconds
     *
     * @return
     */
    public static long getCurrentTimeInLong() {
        return System.currentTimeMillis();
    }

    /**
     * get current time in milliseconds, format is {@link #DEFAULT_DATE_FORMAT}
     *
     * @return
     */
    public static String getCurrentTimeInString() {
        return getTime(getCurrentTimeInLong());
    }

    /**
     * get current time in milliseconds
     *
     * @return
     */
    public static String getCurrentTimeInString(SimpleDateFormat dateFormat) {
        return getTime(getCurrentTimeInLong(), dateFormat);
    }

    /**
     * @param elapseTime
     * @param minIndex   0代表显示到毫秒，1秒，2分， 3小时， 4只显示分秒
     * @return
     */
    public static String getElapseTimeInString(long elapseTime, int minIndex) {

        String elapseTimeStr = getElapseTimeInString(elapseTime, minIndex, "");

        return elapseTimeStr;
    }

    public static String getElapseTimeInString(long elapseTime, int minIndex, String format) {
        int hour = (int) (elapseTime / 1000 / 60 / 60);
        int minute = (int) (elapseTime / 1000 / 60 % 60);
        int second = (int) (elapseTime / 1000 % 60);
        int milsecond = (int) (elapseTime % 1000 / 10);


        String elapseTimeStr = "";
        if (minIndex == 0) {
            if (TextUtils.isEmpty(format)) {
                format = "%02d:%02d:02d:%02d";
            }
            elapseTimeStr = String.format(format, hour, minute, second, milsecond);
        } else if (minIndex == 1) {
            if (TextUtils.isEmpty(format)) {
                format = "%02d:%02d:%02d";
            }
            elapseTimeStr = String.format(format, hour, minute, second);
        } else if (minIndex == 2) {
            if (TextUtils.isEmpty(format)) {
                format = "%02d:%02d:%02d";
            }
            elapseTimeStr = String.format(format, minute, second, milsecond);
        } else if (minIndex == 4) {
            if (TextUtils.isEmpty(format)) {
                format = "%02d:%02d";
            }
            elapseTimeStr = String.format(format, minute, second);
        }else if (minIndex == 5) {
            if (TextUtils.isEmpty(format)) {
                format = "%02d:%02d";
            }
            elapseTimeStr = String.format(format, hour, minute);
        }

        return elapseTimeStr;
    }


}
